package com.cooldatasoft.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cooldatasoft.base.RootServiceImpl;
import com.cooldatasoft.data.ComboValue;
import com.cooldatasoft.data.Person;
import com.cooldatasoft.data.PersonNodeMapping;
import com.cooldatasoft.data.PersonProperty;
import com.cooldatasoft.data.PersonPropertyValue;
import com.cooldatasoft.data.User;
import com.cooldatasoft.data.WorkExp;
import com.cooldatasoft.data.WorkExpPropertyValue;
import com.cooldatasoft.exception.WOCException;
import com.cooldatasoft.service.PersonService;

public class PersonServiceImpl extends RootServiceImpl implements PersonService {

	public Log log = LogFactory.getLog(PersonServiceImpl.class);
	
	public List<PersonProperty> getPersonPropertyList() {
		return getPersonPropertyDAO().find("from PersonProperty WHERE deleted = false ORDER BY sequence ASC");
	}

	public void saveOrUpdate(Person entity) {
		getPersonDAO().saveOrUpdate(entity);	
	}
	
	public void saveOrUpdate(PersonPropertyValue entity) {
		getPersonPropertyValueDAO().saveOrUpdate(entity);		
	}

	public PersonPropertyValue getPersonPropertyValue(Integer personId,Integer personPropertyId) {
		List<PersonPropertyValue> personPropertyValueList = getPersonPropertyValueDAO().find("from PersonPropertyValue where " +
				"deleted=false AND personId="+personId+" AND personPropertyId="+personPropertyId);
		if(personPropertyValueList.size()>0){
			return personPropertyValueList.get(0);
		}else{
			return null;
		}
	}

	public List<Person> getPersonList() {
		return getPersonDAO().find("from Person WHERE deleted=false AND id>1");
	}

	public Person get(Integer entityId) {
		return getPersonDAO().get(entityId);
	}

	public void deletePersonById(Integer entityId,User currentUser) {
		Person person = getPersonDAO().get(entityId);
		List<PersonPropertyValue> personPropertyValueList = getPersonPropertyValueDAO().getPersonPropertyValueList(entityId);
		List<WorkExp> tbWorkExpList = getWorkExpDAO().getWorkExpListByPersonId(entityId);
		List<WorkExpPropertyValue> workExpPropertyValueList = getWorkExpPropertyValueDAO().getWorkExpPropertyValueListByWorkExp(tbWorkExpList);
		
		//Mark person as deleted
		person.setDeleted(true);
		person.setUpdateDate(new Date());
		person.setUpdateUserId(currentUser.getId());
		getPersonDAO().saveOrUpdate(person);
		
		//mark personal info of this person as deleted
		for (Iterator<PersonPropertyValue> iterator = personPropertyValueList.iterator(); iterator.hasNext();) {
			PersonPropertyValue tbPersonPropertyValue = iterator.next();
			tbPersonPropertyValue.setDeleted(true);
			tbPersonPropertyValue.setUpdateDate(new Date());
			tbPersonPropertyValue.setUpdateUserId(currentUser.getId());
			getPersonPropertyValueDAO().saveOrUpdate(tbPersonPropertyValue);			
		}
		
		//Mark WorkExp of this person as deleted
		for (Iterator<WorkExp> iterator = tbWorkExpList.iterator(); iterator.hasNext();) {
			WorkExp tbWorkExp = iterator.next();
			tbWorkExp.setDeleted(true);
			tbWorkExp.setUpdateDate(new Date());
			tbWorkExp.setUpdateUserId(currentUser.getId());
			getWorkExpDAO().saveOrUpdate(tbWorkExp);
		}
		//Mark WorkExpPropertyValue of the WorkExp's of this person as deleted 
		for (Iterator<WorkExpPropertyValue> iterator = workExpPropertyValueList.iterator(); iterator.hasNext();) {
			WorkExpPropertyValue tbWorkExpPropertyValue = iterator.next();
			tbWorkExpPropertyValue.setDeleted(true);
			tbWorkExpPropertyValue.setUpdateDate(new Date());
			tbWorkExpPropertyValue.setUpdateUserId(currentUser.getId());
			getWorkExpPropertyValueDAO().saveOrUpdate(tbWorkExpPropertyValue);
		}
		
	}

	public void deletePersonPropertyById(Integer entityId, User currentUser) {
		PersonProperty entity = getPersonPropertyDAO().get(entityId);		
		entity.setUpdateDate(new Date());
		entity.setUpdateUserId(currentUser.getId());
		entity.setDeleted(true);	
		getPersonPropertyDAO().saveOrUpdate(entity);		
	}

	public void saveOrUpdate(PersonProperty personProperty) {
		getPersonPropertyDAO().saveOrUpdate(personProperty);
	}

	public PersonProperty getPersonPropertyById(Integer entityId) {
		return getPersonPropertyDAO().get(entityId);
	}

	public PersonPropertyValue getPersonPropertyValueById(Integer personId,	int personPropertyId) {
		return getPersonPropertyValueDAO().getValueByPersonId(personId,personPropertyId);
	}

	public List<ComboValue> getPersonSelectOptions() {
		
		List<Person> personList = getPersonList();
		List<ComboValue> optionList = new ArrayList<ComboValue>();
		
		
		for(Person person:personList){
			PersonPropertyValue name = getPersonPropertyValueById(person.getId(), 1);
			PersonPropertyValue surname = getPersonPropertyValueById(person.getId(), 2);
			if(name == null || surname == null){
				System.out.println();
			}
			optionList.add(new ComboValue(person.getId(),name.getValue()+" "+surname.getValue()));
		}
		
		return optionList;
		
	}

	public String getName(Integer personId, Boolean isCMS){
		if(personId== null || personId == 1){
			if(isCMS){
				return "";
			}else{
				throw new WOCException("Please use CMS to insert data into the system!");
			}
		}
		return getPersonPropertyValue(personId, 1).getValue();
	}
	public String getSurname(Integer personId, Boolean isCMS){
		if(personId== null || personId == 1){
			if(isCMS){
				return "";
			}else{
				throw new WOCException("Please use CMS to insert data into the system!");
			}
		}
		return getPersonPropertyValue(personId, 2).getValue();
	}
	
	/**
	 * Returns People in this node.
	 */
	public List<Person> getPersonList(Integer nodeId) {
		List<PersonNodeMapping> personNodeMappingList = 
			getPersonNodeMappingDAO().find("from PersonNodeMapping where nodeId = "+nodeId);
		List<Person> personList = new ArrayList<Person>();
		
		for (PersonNodeMapping personNodeMapping:personNodeMappingList) {
			personList.add(	get(personNodeMapping.getPersonId()) );		
		}
		return personList;
	}

	public String getPosition(Integer personId, Boolean isCMS) {
		if(personId== null || personId == 1){
			if(isCMS){
				return "ROOT";
			}else{
				throw new WOCException("Please use CMS to insert data into the system!");
			}
		}
		return getPersonPropertyValue(personId, 3).getValue();
	}

}
